package com.ma.web.admin;


import com.ma.po.Blog;
import com.ma.po.User;
import com.ma.service.BlogService;
import com.ma.service.TagService;
import com.ma.service.TypeService;
import com.ma.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/admin")
public class BlogController {

    private static final String INPUT = "admin/blogs-input";
    private static final String LIST = "admin/blogs";
    private static final String REDIRECT_LIST = "redirect:/admin/blogs";

    @Autowired
    private BlogService blogService;
    //在博客首页得到所有的分类，便于搜索时下拉展示
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;

    @GetMapping("/blogs")
    public String blogs(
            @PageableDefault(size=3,sort={"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
            BlogQuery blog, Model model){

        model.addAttribute("page",blogService.listBlog(pageable,blog));

        //添加所有分类
        model.addAttribute("types",typeService.listTypes());

        return LIST;
    }

    @PostMapping("/blogs/search")
    public String search(
            @PageableDefault(size=3,sort={"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
            BlogQuery blog, Model model){

        model.addAttribute("page",blogService.listBlog(pageable,blog));

        //返回blogs页面中的blogList片段
        return "admin/blogs :: blogList";
    }


    //新增
    @GetMapping("/blogs/input")
    public String input(Model model){
        setTypeAndTag(model);
        model.addAttribute("blog",new Blog());
        return INPUT;
    }

    private void setTypeAndTag(Model model){
        model.addAttribute("types",typeService.listTypes());
        model.addAttribute("tags",tagService.listTags());

    }


    //修改
    @GetMapping("/blogs/{id}/input")
    public String editInput(@PathVariable Long id, Model model){
        setTypeAndTag(model);
        Blog blog = blogService.getBlog(id);
        blog.init();
        model.addAttribute("blog",blog);
        return INPUT;
    }

    @PostMapping("/blogs")
    //将新增与编辑写到一起
    public String post(Blog blog, HttpSession session,RedirectAttributes redirectAttributes){
        //blog初始化的参数
        blog.setUser((User) session.getAttribute("user"));
        blog.setType(typeService.getType(blog.getType().getId()));
        blog.setTags(tagService.listTags(blog.getTagIds()));
        //Blog b = blogService.saveBlog(blog);
        Blog b;
        if (blog.getId()==null){
            b=blogService.saveBlog(blog);
        }else {
            b=blogService.updateBlog(blog.getId(),blog);
        }

        if(b != null)
            redirectAttributes.addFlashAttribute("message","操作成功");
        else
            redirectAttributes.addFlashAttribute("message","操作失败");
        return REDIRECT_LIST;
    }

    @GetMapping("/blogs/{id}/delete")
    public String delete(@PathVariable Long id, RedirectAttributes attributes){
        blogService.deleteBlog(id);
        attributes.addFlashAttribute("message","删除成功");
        return  REDIRECT_LIST;
    }
}
